package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.FieldValOptionVo;
import com.zb.entity.ZjSpareParamFieldVal;
import com.zb.entity.ZjSpareParamFieldValOption;
import com.zb.entity.ZjSpareRelationParam;
import com.zb.extend.SpareParamFieldValExtend;
import com.zb.mapper.ZjSpareParamFieldValMapper;
import com.zb.service.ZjSpareParamFieldValOptionService;
import com.zb.service.ZjSpareParamFieldValService;
import com.zb.vo.GoodsFieldValVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 备件参数字段值关联表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-18
 */
@Service
public class ZjSpareParamFieldValServiceImpl extends ServiceImpl<ZjSpareParamFieldValMapper, ZjSpareParamFieldVal> implements ZjSpareParamFieldValService {
    @Resource
    private ZjSpareParamFieldValOptionService spareParamFieldValOptionService;

    // 保存备件参数字段
    @Override
    @Transactional
    public void save(Long custAccId, Long spareRelationParamId, List<GoodsFieldValVo> fieldValVos) {
        if (null == fieldValVos || fieldValVos.isEmpty()) {
            return;
        }
        spareParamFieldValOptionService.deleteBySpareRelationParamId(spareRelationParamId);
        List<ZjSpareParamFieldVal> adds = new ArrayList<>();
        List<ZjSpareParamFieldVal> updates = new ArrayList<>();
        ZjSpareParamFieldVal fieldVal = null;
        List<ZjSpareParamFieldValOption> addOptVals = new ArrayList<>();
        ZjSpareParamFieldValOption option = null;
        for (GoodsFieldValVo vo : fieldValVos) {
            fieldVal = new ZjSpareParamFieldVal();
            BeanUtils.copyProperties(vo, fieldVal);
            if (null != fieldVal.getId()) {
                updates.add(fieldVal);
            } else {
                fieldVal.setId(IdWorker.getId());
                fieldVal.setCustAccId(custAccId);
                fieldVal.setSpareRelationParamId(spareRelationParamId);
                adds.add(fieldVal);
            }
            for (FieldValOptionVo optVo : vo.getOptVos()) {
                option = new ZjSpareParamFieldValOption();
                BeanUtil.copyProperties(optVo, option);
                option.setId(IdWorker.getId());
                option.setCustAccId(custAccId);
                option.setSpareRelationParamId(spareRelationParamId);
                option.setSpareParamFieldValId(fieldVal.getId());
                addOptVals.add(option);
            }
        }
        if (CollectionUtil.isNotEmpty(adds))this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates))this.updateBatchById(updates);
        if (CollectionUtil.isNotEmpty(addOptVals))spareParamFieldValOptionService.saveBatch(addOptVals);
    }

    // 根据备件参数ID获取备件参数字段
    @Override
    public List<SpareParamFieldValExtend> getBySpareRelationParamId(Long spareRelationParamId) {
        QueryWrapper<ZjSpareParamFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("id", "goods_param_field_id", "type", "name");
        wrapper.eq("spare_relation_param_id", spareRelationParamId);
        List<ZjSpareParamFieldVal> fieldValList = this.list(wrapper);
        List<ZjSpareParamFieldValOption> options = spareParamFieldValOptionService.getbySpareParamFieldValIds(fieldValList.stream().map(ZjSpareParamFieldVal::getId).toList());
        return fieldValList.stream().map(fieldVal -> {
            SpareParamFieldValExtend extend = new SpareParamFieldValExtend();
            BeanUtil.copyProperties(fieldVal, extend);
            extend.setOptVos(options.stream().filter(f -> Objects.equals(f.getSpareParamFieldValId(), fieldVal.getId())).map(opt -> {
                return new FieldValOptionVo(opt.getId(), opt.getOptValue());
            }).toList());
            return extend;
        }).toList();
    }

    // 根据备件参数ID删除参数字段
    @Override
    public void deleteBatchBySpareRelationParamIds(List<Long> spareRelationParamIds) {
        if (CollectionUtils.isEmpty(spareRelationParamIds)) {
            return;
        }
        spareParamFieldValOptionService.deleteBySpareRelationParamIds(spareRelationParamIds);
        QueryWrapper<ZjSpareParamFieldVal> wrapper = new QueryWrapper<>();
        wrapper.in("spare_relation_param_id", spareRelationParamIds);
        this.remove(wrapper);
    }

    // 根据商品参数字段ID查询
    @Override
    public List<Long> getByGoodsParamFieldIds(List<Long> goodsParamFieldIds) {
        if (CollectionUtils.isEmpty(goodsParamFieldIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjSpareParamFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("goods_param_field_id");
        wrapper.in("goods_param_field_id", goodsParamFieldIds);
        return this.list(wrapper).stream().map(ZjSpareParamFieldVal::getGoodsParamFieldId).distinct().toList();
    }
}
